package server.life;


import constants.GameConstants;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import provider.*;
import tools.Pair;
import tools.Randomizer;
import tools.StringUtil;

import java.awt.*;
import java.io.File;
import java.util.*;
import java.util.List;


public class MapleLifeFactory {
    /*  20 */   private static final MapleDataProvider data = MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/Mob.wz"));
    /*  21 */   private static final MapleDataProvider data2 = MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/Mob2.wz"));
    /*  22 */   private static final MapleDataProvider npcData = MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/Npc.wz"));
    /*  23 */   private static final MapleDataProvider stringDataWZ = MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/String.wz"));
    /*  24 */   private static final MapleDataProvider etcDataWZ = MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/Etc.wz"));
    /*  25 */   private static final Logger log = LogManager.getLogger();
    /*  26 */   private static final MapleData mobStringData = stringDataWZ.getData("Mob.img");

    /*  28 */   private static final MapleData npclocData = etcDataWZ.getData("NpcLocation.img");


    /*  34 */   private static final Map<Integer, String> allNpcNames = new HashMap<>();
    /*  35 */   private static final Map<Integer, String> allMobNames = new HashMap<>();
    /*  36 */   private static final Map<Integer, Boolean> allMobID = new HashMap<>();
    /*  37 */   private static final Map<Integer, MapleMonsterStats> monsterStats = new HashMap<>();
    /*  38 */   private static final Map<Integer, Integer> NPCLoc = new HashMap<>();
    /*  39 */   private static final Map<Integer, List<Integer>> questCount = new HashMap<>();


    public static void loadMonsterName() {
        /*  43 */
        for (MapleData mapleData : mobStringData) {
            /*  44 */
            allMobNames.put(Integer.valueOf(mapleData.getName()), String.valueOf(mapleData.getChildByPath("name").getData()));
        }
    }

    public static void loadMonsterID() {
        /*  49 */
        List<MapleDataProvider> datas = Arrays.asList(new MapleDataProvider[]{data, data2});
        /*  50 */
        for (MapleDataProvider root : datas) {
            /*  51 */
            for (MapleDataFileEntry mapleDataFileEntry : root.getRoot().getFiles()) {
                /*  52 */
                int mobid = Integer.valueOf(mapleDataFileEntry.getName().substring(0, mapleDataFileEntry.getName().indexOf(".img"))).intValue();
                /*  53 */
                boolean isboss = (MapleDataTool.getInt("info/boss", root.getData(mapleDataFileEntry.getName()), 0) > 0);
                /*  54 */
                allMobID.put(Integer.valueOf(mobid), Boolean.valueOf(isboss));
            }
        }
    }


    public static AbstractLoadedMapleLife getLife(int id, String type, int mapid) {
        /*  82 */
        if (type.equalsIgnoreCase("n"))
            /*  83 */ return getNPC(id, mapid);
        /*  84 */
        if (type.equalsIgnoreCase("m")) {
            /*  85 */
            return getMonster(id);
        }
        /*  87 */
        System.err.println("Unknown Life type: " + type + "");
        /*  88 */
        return null;
    }


    public static String getMonsterName(int mobid) {
        /*  93 */
        return allMobNames.get(Integer.valueOf(mobid));
    }

    public static Map<Integer, String> getAllMonsterName() {
        /*  97 */
        return allMobNames;
    }

    public static boolean isBoss(int mobid) {
        /* 101 */
        return ((Boolean) allMobID.get(Integer.valueOf(mobid))).booleanValue();
    }

    public static boolean checkMonsterIsExist(int mobid) {
        /* 105 */
        return allMobID.containsKey(Integer.valueOf(mobid));
    }

    public static int getNPCLocation(int npcid) {
        /* 109 */
        if (NPCLoc.containsKey(Integer.valueOf(npcid))) {
            /* 110 */
            return ((Integer) NPCLoc.get(Integer.valueOf(npcid))).intValue();
        }
        /* 112 */
        int map = MapleDataTool.getIntConvert(Integer.toString(npcid) + "/0", npclocData, -1);
        /* 113 */
        NPCLoc.put(Integer.valueOf(npcid), Integer.valueOf(map));
        /* 114 */
        return map;
    }

    public static void loadQuestCounts() {
        /* 118 */
        for (int i = 0; i < 2; i++) {
            /* 119 */
            for (MapleDataDirectoryEntry mapz : (i == 0) ? data.getRoot().getSubdirectories() : data2.getRoot().getSubdirectories()) {
                /* 120 */
                if (mapz.getName().equals("QuestCountGroup")) {
                    /* 121 */
                    for (MapleDataFileEntry entry : mapz.getFiles()) {
                        /* 122 */
                        int id = Integer.parseInt(entry.getName().substring(0, entry.getName().length() - 4));
                        /* 123 */
                        MapleData dat = data.getData("QuestCountGroup/" + entry.getName());
                        /* 124 */
                        if (dat != null && dat.getChildByPath("info") != null) {
                            /* 125 */
                            List<Integer> info = new ArrayList<>();
                            /* 126 */
                            for (MapleData da : dat.getChildByPath("info")) {
                                /* 127 */
                                info.add(Integer.valueOf(MapleDataTool.getInt(da, 0)));
                            }
                            /* 129 */
                            questCount.put(Integer.valueOf(id), info);
                        }
                    }
                }
            }
        }
        /* 135 */
        MapleData npcStringData = stringDataWZ.getData("Npc.img");
        /* 136 */
        for (MapleData c : npcStringData) {
            /* 137 */
            if (c.getName().contains("pack_ignore")) {
                continue;
            }
            /* 140 */
            int nid = Integer.parseInt(c.getName());
            /* 141 */
            String n = StringUtil.getLeftPaddedStr(nid + ".img", '0', 11);
            try {
                /* 143 */
                if (npcData.getData(n) != null) {
                    /* 144 */
                    String name = MapleDataTool.getString("name", c, "MISSINGNO");
                    /* 145 */
                    if (name.contains("MapleTV") || name.contains("婴儿月妙")) {
                        continue;
                    }
                    /* 148 */
                    allNpcNames.put(Integer.valueOf(nid), name);
                }
                /* 150 */
            } catch (RuntimeException ex) {
                /* 151 */
                log.error("", ex);
            }
        }
    }

    public static boolean exitsQuestCount(int mo, int id) {
        /* 157 */
        if (questCount.containsKey(Integer.valueOf(mo))) {
            /* 158 */
            return ((List) questCount.get(Integer.valueOf(mo))).contains(Integer.valueOf(id));
        }
        /* 160 */
        return false;
    }

    public static MapleMonster getMonster(int mobId) {
        /* 164 */
        MapleMonsterStats stats = getMonsterStats(mobId);
        /* 165 */
        if (stats == null) {
            /* 166 */
            return null;
        }
        /* 168 */
        return new MapleMonster(mobId, stats);
    }

    public static MapleMonsterStats getMonsterStats(int mobId) {
        /* 172 */
        MapleMonsterStats stats = monsterStats.get(Integer.valueOf(mobId));
        /* 173 */
        boolean secondData = false;

        /* 175 */
        if (stats == null) {


            /* 178 */
            MapleData monsterData = MapleLifeFactory.data.getData(StringUtil.getLeftPaddedStr(Integer.toString(mobId) + ".img", '0', 11));
            /* 179 */
            if (monsterData == null) {
                /* 180 */
                monsterData = data2.getData(StringUtil.getLeftPaddedStr(Integer.toString(mobId) + ".img", '0', 11));
                /* 181 */
                if (monsterData == null) {
                    /* 182 */
                    return null;
                }
                /* 184 */
                secondData = true;
            }

            /* 187 */
            MapleData monsterInfoData = monsterData.getChildByPath("info");
            /* 188 */
            stats = new MapleMonsterStats(mobId);

            /* 190 */
            if (monsterInfoData.getChildByPath("maxHP").getData().toString().contains("?")) {

                /* 192 */
                stats.setHp(2147483647L);
            } else {
                /* 194 */
                stats.setHp((GameConstants.getPartyPlayHP(mobId) > 0) ? GameConstants.getPartyPlayHP(mobId) : MapleDataTool.getLong("maxHP", monsterInfoData));
            }
            /* 196 */
            stats.setMp(MapleDataTool.getIntConvert("maxMP", monsterInfoData, 0));
            /* 197 */
            stats.setExp((mobId == 9300027) ? 0 : ((GameConstants.getPartyPlayEXP(mobId) > 0) ? GameConstants.getPartyPlayEXP(mobId) : MapleDataTool.getIntConvert("exp", monsterInfoData, 0)));
            /* 198 */
            stats.setLevel((short) MapleDataTool.getIntConvert("level", monsterInfoData, 1));
            /* 199 */
            stats.setWeaponPoint((short) MapleDataTool.getIntConvert("wp", monsterInfoData, 0));
            /* 200 */
            stats.setCharismaEXP((short) MapleDataTool.getIntConvert("charismaEXP", monsterInfoData, 0));
            /* 201 */
            stats.setRemoveAfter(MapleDataTool.getIntConvert("removeAfter", monsterInfoData, 0));
            /* 202 */
            stats.setrareItemDropLevel((byte) MapleDataTool.getIntConvert("rareItemDropLevel", monsterInfoData, 0));
            /* 203 */
            stats.setFixedDamage(MapleDataTool.getIntConvert("fixedDamage", monsterInfoData, -1));
            /* 204 */
            stats.setOnlyNormalAttack((MapleDataTool.getIntConvert("onlyNormalAttack", monsterInfoData, 0) > 0));
            /* 205 */
            stats.setBoss((GameConstants.getPartyPlayHP(mobId) > 0 || MapleDataTool.getIntConvert("boss", monsterInfoData, 0) > 0 || mobId == 8810018 || mobId == 9410066 || (mobId >= 8810118 && mobId <= 8810122)));
            /* 206 */
            stats.setExplosiveReward((MapleDataTool.getIntConvert("explosiveReward", monsterInfoData, 0) > 0));
            /* 207 */
            stats.setUndead((MapleDataTool.getIntConvert("undead", monsterInfoData, 0) > 0));
            /* 208 */
            stats.setEscort((MapleDataTool.getIntConvert("escort", monsterInfoData, 0) > 0));
            /* 209 */
            stats.setPartyBonus((GameConstants.getPartyPlayHP(mobId) > 0 || MapleDataTool.getIntConvert("partyBonusMob", monsterInfoData, 0) > 0));
            /* 210 */
            stats.setPartyBonusRate(MapleDataTool.getIntConvert("partyBonusR", monsterInfoData, 0));
            /* 211 */
            if (mobStringData.getChildByPath(String.valueOf(mobId)) != null) {
                /* 212 */
                stats.setName(MapleDataTool.getString("name", mobStringData.getChildByPath(String.valueOf(mobId)), "MISSINGNO"));
            }
            /* 214 */
            stats.setBuffToGive(MapleDataTool.getIntConvert("buff", monsterInfoData, -1));
            /* 215 */
            stats.setChange((MapleDataTool.getIntConvert("changeableMob", monsterInfoData, 0) > 0));
            /* 216 */
            stats.setFriendly((MapleDataTool.getIntConvert("damagedByMob", monsterInfoData, 0) > 0));
            /* 217 */
            stats.setNoDoom((MapleDataTool.getIntConvert("noDoom", monsterInfoData, 0) > 0));
            /* 218 */
            stats.setFfaLoot((MapleDataTool.getIntConvert("publicReward", monsterInfoData, 0) > 0));
            /* 219 */
            stats.setCP((byte) MapleDataTool.getIntConvert("getCP", monsterInfoData, 0));
            /* 220 */
            stats.setPoint(MapleDataTool.getIntConvert("point", monsterInfoData, 0));
            /* 221 */
            stats.setDropItemPeriod(MapleDataTool.getIntConvert("dropItemPeriod", monsterInfoData, 0));
            /* 222 */
            stats.setPhysicalAttack(MapleDataTool.getIntConvert("PADamage", monsterInfoData, 0));
            /* 223 */
            stats.setMagicAttack(MapleDataTool.getIntConvert("MADamage", monsterInfoData, 0));
            /* 224 */
            stats.setPDRate(MapleDataTool.getIntConvert("PDRate", monsterInfoData, 0));
            /* 225 */
            stats.setMDRate(MapleDataTool.getIntConvert("MDRate", monsterInfoData, 0));
            /* 226 */
            stats.setAcc(MapleDataTool.getIntConvert("acc", monsterInfoData, 0));
            /* 227 */
            stats.setEva(MapleDataTool.getIntConvert("eva", monsterInfoData, 0));
            /* 228 */
            stats.setSummonType((byte) MapleDataTool.getIntConvert("summonType", monsterInfoData, 0));
            /* 229 */
            stats.setCategory((byte) MapleDataTool.getIntConvert("category", monsterInfoData, 0));
            /* 230 */
            stats.setSpeed(MapleDataTool.getIntConvert("speed", monsterInfoData, 0));
            /* 231 */
            stats.setPushed(MapleDataTool.getIntConvert("pushed", monsterInfoData, 0));

            /* 233 */
            stats.setRemoveOnMiss((MapleDataTool.getIntConvert("removeOnMiss", monsterInfoData, 0) > 0));
            /* 234 */
            stats.setSkeleton((MapleDataTool.getIntConvert("skeleton", monsterInfoData, 0) > 0));
            /* 235 */
            stats.setInvincible((MapleDataTool.getIntConvert("invincible", monsterInfoData, 0) > 0));
            /* 236 */
            stats.setSmartPhase(MapleDataTool.getIntConvert("smartPhase", monsterInfoData, 0));
            /* 237 */
            stats.setIgnoreMoveImpact((MapleDataTool.getIntConvert("ignoreMoveImpact", monsterInfoData, 0) > 0));

            /* 239 */
            MapleData special = monsterInfoData.getChildByPath("coolDamage");
            /* 240 */
            if (special != null) {
                /* 241 */
                int coolDmg = MapleDataTool.getIntConvert("coolDamage", monsterInfoData);
                /* 242 */
                int coolProb = MapleDataTool.getIntConvert("coolDamageProb", monsterInfoData, 0);
                /* 243 */
                stats.setCool(new Pair(Integer.valueOf(coolDmg), Integer.valueOf(coolProb)));
            }
            /* 245 */
            special = monsterInfoData.getChildByPath("loseItem");
            /* 246 */
            if (special != null) {
                /* 247 */
                for (MapleData liData : special.getChildren()) {
                    /* 248 */
                    stats.addLoseItem(new loseItem(MapleDataTool.getInt(liData.getChildByPath("id")), (byte) MapleDataTool.getInt(liData.getChildByPath("prop")), (byte) MapleDataTool.getInt(liData.getChildByPath("x"))));
                }
            }

            /* 252 */
            MapleData patrol = monsterInfoData.getChildByPath("patrol");
            /* 253 */
            if (patrol != null) {
                /* 254 */
                stats.setHasRange(true);
                /* 255 */
                stats.setRange(MapleDataTool.getIntConvert("range", patrol, 0));
                /* 256 */
                stats.setDetectX(MapleDataTool.getIntConvert("detectX", patrol, stats.getRemoveAfter()));
                /* 257 */
                stats.setSenseX((byte) MapleDataTool.getIntConvert("senseX", patrol, -1));
            } else {
                /* 259 */
                stats.setSelfD((byte) -1);
            }

            /* 262 */
            MapleData selfd = monsterInfoData.getChildByPath("selfDestruction");
            /* 263 */
            if (selfd != null) {
                /* 264 */
                stats.setSelfDHP(MapleDataTool.getIntConvert("hp", selfd, 0));
                /* 265 */
                stats.setRemoveAfter(MapleDataTool.getIntConvert("removeAfter", selfd, stats.getRemoveAfter()));
                /* 266 */
                stats.setSelfD((byte) MapleDataTool.getIntConvert("action", selfd, -1));
            } else {
                /* 268 */
                stats.setSelfD((byte) -1);
            }

            /* 271 */
            MapleData firstAttackData = monsterInfoData.getChildByPath("firstAttack");
            /* 272 */
            int firstAttack = 0;
            /* 273 */
            if (firstAttackData != null) {
                /* 274 */
                if (firstAttackData.getType() == MapleDataType.FLOAT) {
                    /* 275 */
                    firstAttack = Math.round(MapleDataTool.getFloat(firstAttackData));
                } else {
                    /* 277 */
                    firstAttack = MapleDataTool.getInt(firstAttackData);
                }
            }
            /* 280 */
            stats.setFirstAttack((firstAttack > 0));

            /* 282 */
            if (stats.isBoss() || isDmgSponge(mobId)) {
                /* 283 */
                if (monsterInfoData.getChildByPath("hpTagColor") == null || monsterInfoData.getChildByPath("hpTagBgcolor") == null) {
                    /* 284 */
                    stats.setTagColor(0);
                    /* 285 */
                    stats.setTagBgColor(0);
                } else {
                    /* 287 */
                    stats.setTagColor(MapleDataTool.getIntConvert("hpTagColor", monsterInfoData));
                    /* 288 */
                    stats.setTagBgColor(MapleDataTool.getIntConvert("hpTagBgcolor", monsterInfoData));
                }
            }

            /* 292 */
            MapleData banishData = monsterInfoData.getChildByPath("ban");
            /* 293 */
            if (banishData != null) {
                /* 294 */
                ArrayList<BanishInfo> banishInfos = new ArrayList<>();
                /* 295 */
                MapleData banMsg1 = banishData.getChildByPath("banMsg");
                /* 296 */
                if (banMsg1 != null) {
                    /* 297 */
                    for (MapleData banMsg : banMsg1.getChildren()) {
                        /* 298 */
                        banishInfos.add(new BanishInfo(
                                /* 299 */                   MapleDataTool.getString("banMsg", banishData, ""),
                                /* 300 */                   MapleDataTool.getInt("field", banMsg, -1),
                                /* 301 */                   MapleDataTool.getString("portal", banMsg, "sp")));
                    }

                    /* 304 */
                    stats.setBanishInfo(banishInfos);
                }
            }

            /* 308 */
            MapleData reviveInfo = monsterInfoData.getChildByPath("revive");
            /* 309 */
            if (reviveInfo != null) {
                /* 310 */
                List<Integer> revives = new LinkedList<>();
                /* 311 */
                for (MapleData bdata : reviveInfo) {
                    /* 313 */
                    revives.add(Integer.valueOf(MapleDataTool.getInt(bdata)));
                }
                /* 315 */
                stats.setRevives(revives);
            }

            /* 318 */
            MapleData mobZoneInfo = monsterInfoData.getChildByPath("mobZone");
            /* 319 */
            if (mobZoneInfo != null) {
                /* 320 */
                List<Pair<Point, Point>> mobZone = new LinkedList<>();
                /* 321 */
                for (MapleData bdata : mobZoneInfo) {
                    /* 322 */
                    mobZone.add(new Pair(bdata.getChildByPath("lt").getData(), bdata.getChildByPath("rb").getData()));
                }
                /* 324 */
                stats.setMobZone(mobZone);
            }

            /* 327 */
            MapleData trans = monsterInfoData.getChildByPath("trans");
            /* 328 */
            MapleMonsterStats.TransMobs transMobs = null;
            /* 329 */
            if (trans != null) {
                /* 330 */
                List<Integer> mobids = new ArrayList<>();
                /* 331 */
                List<Pair<Integer, Integer>> arrayList = new ArrayList<>();
                /* 332 */
                if (trans.getChildByPath("0") != null) {
                    /* 333 */
                    mobids.add(Integer.valueOf(MapleDataTool.getInt(trans.getChildByPath("0"), -1)));
                }
                /* 335 */
                if (trans.getChildByPath("1") != null) {
                    /* 336 */
                    mobids.add(Integer.valueOf(MapleDataTool.getInt(trans.getChildByPath("1"), -1)));
                }
                /* 338 */
                int time = MapleDataTool.getInt(trans.getChildByPath("time"), 0);
                /* 339 */
                int cooltime = MapleDataTool.getInt(trans.getChildByPath("cooltime"), 0);
                /* 340 */
                int hpTriggerOn = MapleDataTool.getInt(trans.getChildByPath("hpTriggerOn"), 0);
                /* 341 */
                int hpTriggerOff = MapleDataTool.getInt(trans.getChildByPath("hpTriggerOff"), 0);
                /* 342 */
                int withMob = MapleDataTool.getInt(trans.getChildByPath("withMob"), -1);
                /* 343 */
                if (trans.getChildByPath("skill") != null) {
                    /* 344 */
                    for (MapleData data : trans.getChildByPath("skill")) {
                        /* 345 */
                        arrayList.add(new Pair(Integer.valueOf(MapleDataTool.getInt("skill", data, 0)), Integer.valueOf(MapleDataTool.getInt("level", data, 0))));
                    }
                }
                /* 348 */
                transMobs = new MapleMonsterStats.TransMobs(mobids, arrayList, time, cooltime, hpTriggerOn, hpTriggerOff, withMob);
            }
            /* 350 */
            stats.setTransMobs(transMobs);















            /* 366 */
            MapleData monsterSkillData = monsterInfoData.getChildByPath("skill");
            /* 367 */
            if (monsterSkillData != null) {
                /* 368 */
                int i = 0;
                /* 369 */
                List<Pair<Integer, Integer>> skills = new ArrayList<>();
                /* 370 */
                while (monsterSkillData.getChildByPath(Integer.toString(i)) != null) {
                    /* 371 */
                    skills.add(new Pair(Integer.valueOf(MapleDataTool.getInt(i + "/skill", monsterSkillData, 0)), Integer.valueOf(MapleDataTool.getInt(i + "/level", monsterSkillData, 0))));
                    /* 372 */
                    if (MapleDataTool.getInt(i + "/afterAttack", monsterSkillData, -1) != -1) {
                        /* 373 */
                        stats.addAfterAttack(((Integer) ((Pair) skills.get(i)).left).intValue(), ((Integer) ((Pair) skills.get(i)).right).intValue(), MapleDataTool.getInt(i + "/afterAttack", monsterSkillData, -1));
                    }
                    /* 375 */
                    if (MapleDataTool.getInt(i + "/skillAfter", monsterSkillData, -1) != -1) {
                        /* 376 */
                        stats.addSkillAfter(((Integer) ((Pair) skills.get(i)).left).intValue(), ((Integer) ((Pair) skills.get(i)).right).intValue(), MapleDataTool.getInt(i + "/skillAfter", monsterSkillData, -1));
                    }

                    /* 379 */
                    i++;
                }
                /* 381 */
                stats.setSkills(skills);
            }




            /* 387 */
            MapleData monsterAttackData = monsterInfoData.getChildByPath("attack");
            /* 388 */
            if (monsterAttackData != null) {
                /* 389 */
                int i = 0;
                /* 390 */
                List<MobAttack> attacks = new ArrayList<>();
                /* 391 */
                while (monsterAttackData.getChildByPath(Integer.toString(i)) != null) {
                    /* 392 */
                    MobAttack attack = new MobAttack();
                    /* 393 */
                    attack.setAction(MapleDataTool.getInt(i + "/action", monsterAttackData, -1));
                    /* 394 */
                    attack.setAfterAttack(MapleDataTool.getInt(i + "/afterAttack", monsterAttackData, -1));
                    /* 395 */
                    attack.setAttackRatio(MapleDataTool.getInt(i + "/attackRatio", monsterAttackData, -1));
                    /* 396 */
                    attack.setType(MapleDataTool.getInt(i + "/type", monsterAttackData, -1));
                    /* 397 */
                    attack.setAfterDelay(MapleDataTool.getInt(i + "/afterDelay", monsterAttackData, -1));
                    /* 398 */
                    attack.setIgnoreStance(MapleDataTool.getInt(i + "/ignoreStance", monsterAttackData, -1));
                    /* 399 */
                    attack.setFixDamR(MapleDataTool.getInt(i + "/fixDamR", monsterAttackData, -1));
                    /* 400 */
                    attack.setNotMissAttack(MapleDataTool.getInt(i + "/notMissAttack", monsterAttackData, -1));
                    /* 401 */
                    attack.setFixAttack(MapleDataTool.getInt(i + "/fixAttack", monsterAttackData, -1));
                    /* 402 */
                    attack.setCooltime(MapleDataTool.getInt(i + "/cooltime", monsterAttackData, -1));
                    /* 403 */
                    attack.setOnlyAfterAttack(MapleDataTool.getInt(i + "/onlyAfterAttack", monsterAttackData, -1));
                    /* 404 */
                    attack.setDisease(MapleDataTool.getInt(i + "/disease", monsterAttackData, -1));
                    /* 405 */
                    attack.setLevel(MapleDataTool.getInt(i + "/level", monsterAttackData, -1));
                    /* 406 */
                    attack.setAttackCount(MapleDataTool.getInt(i + "/attackCount", monsterAttackData, -1));
                    /* 407 */
                    attack.setDoFirst(MapleDataTool.getInt(i + "/doFirst", monsterAttackData, -1));

                    /* 409 */
                    if (monsterAttackData.getChildByPath(Integer.toString(i) + "/callSkill") != null) {
                        /* 410 */
                        MapleData callSkillData = monsterAttackData.getChildByPath(Integer.toString(i) + "/callSkill");
                        /* 411 */
                        int j = 0;
                        /* 412 */
                        while (callSkillData.getChildByPath(String.valueOf(j)) != null) {
                            /* 413 */
                            MapleData callSkillIdxData = callSkillData.getChildByPath(String.valueOf(j));
                            /* 414 */
                            attack.addSkill(MapleDataTool.getInt("skill", callSkillIdxData, 0), MapleDataTool.getInt("level", callSkillIdxData, 0));
                            /* 415 */
                            j++;
                        }
                    }
                    /* 418 */
                    attacks.add(attack);
                    /* 419 */
                    i++;
                }
                /* 421 */
                stats.setAttacks(attacks);
            }

            /* 424 */
            MapleData monsterHitPartsToSlot = monsterData.getChildByPath("HitParts");
            /* 425 */
            if (monsterHitPartsToSlot != null && monsterHitPartsToSlot.getChildren().size() > 0) {
                /* 426 */
                for (MapleData hitparts : monsterHitPartsToSlot) {
                    /* 427 */
                    int data = 0;
                    /* 428 */
                    Iterator<MapleData> iterator = hitparts.iterator();
                    /* 429 */
                    if (iterator.hasNext()) {
                        /* 430 */
                        data = MapleDataTool.getInt("durability", iterator.next(), 0);
                    }
                    /* 432 */
                    stats.getHitParts().put(hitparts.getName(), Integer.valueOf(data));
                }
            }

            /* 436 */
            decodeElementalString(stats, MapleDataTool.getString("elemAttr", monsterInfoData, ""));

            /* 438 */
            int link = MapleDataTool.getIntConvert("link", monsterInfoData, 0);
            /* 439 */
            stats.setLink(link);
            /* 440 */
            if (link != 0) {
                /* 441 */
                if (!secondData) {
                    /* 442 */
                    monsterData = MapleLifeFactory.data.getData(StringUtil.getLeftPaddedStr(link + ".img", '0', 11));
                } else {
                    /* 444 */
                    monsterData = data2.getData(StringUtil.getLeftPaddedStr(link + ".img", '0', 11));
                }
            }

            /* 448 */
            if (monsterData != null) {
                /* 449 */
                for (MapleData idata : monsterData) {
                    /* 450 */
                    if (!idata.getName().equals("info")) {
                        /* 451 */
                        int delay = 0;
                        /* 452 */
                        for (MapleData pic : idata.getChildren()) {
                            /* 453 */
                            delay += MapleDataTool.getIntConvert("delay", pic, 0);
                        }
                        /* 455 */
                        stats.setAnimationTime(idata.getName(), delay);
                    }
                }

                /* 459 */
                for (int i = 1; ; i++) {
                    /* 460 */
                    MapleData attackData = monsterData.getChildByPath("attack" + i + "/info");
                    /* 461 */
                    if (attackData == null) {
                        break;
                    }
                    /* 464 */
                    MobAttackInfo ret = new MobAttackInfo();
                    /* 465 */
                    ret.setDeadlyAttack((attackData.getChildByPath("deadlyAttack") != null));
                    /* 466 */
                    ret.setMpBurn(MapleDataTool.getInt("mpBurn", attackData, 0));
                    /* 467 */
                    ret.setDiseaseSkill(MapleDataTool.getInt("disease", attackData, 0));
                    /* 468 */
                    ret.setDiseaseLevel(MapleDataTool.getInt("level", attackData, 0));
                    /* 469 */
                    ret.setMpCon(MapleDataTool.getInt("conMP", attackData, 0));
                    /* 470 */
                    ret.attackAfter = MapleDataTool.getInt("attackAfter", attackData, 0);
                    /* 471 */
                    ret.PADamage = MapleDataTool.getInt("PADamage", attackData, 0);
                    /* 472 */
                    ret.MADamage = MapleDataTool.getInt("MADamage", attackData, 0);
                    /* 473 */
                    ret.magic = (MapleDataTool.getInt("magic", attackData, 0) > 0);
                    /* 474 */
                    ret.isElement = (attackData.getChildByPath("elemAttr") != null);
                    /* 475 */
                    if (attackData.getChildByPath("range") != null) {
                        /* 476 */
                        ret.range = MapleDataTool.getInt("range/r", attackData, 0);
                        /* 477 */
                        if (attackData.getChildByPath("range/lt") != null && attackData.getChildByPath("range/rb") != null) {
                            /* 478 */
                            ret.lt = (Point) attackData.getChildByPath("range/lt").getData();
                            /* 479 */
                            ret.rb = (Point) attackData.getChildByPath("range/rb").getData();
                        }
                    }
                    /* 482 */
                    stats.addMobAttack(ret);
                }
            }

            /* 486 */
            byte hpdisplaytype = -1;
            /* 487 */
            if (stats.getTagColor() > 0) {
                /* 488 */
                hpdisplaytype = 0;
                /* 489 */
            } else if (stats.isFriendly()) {
                /* 490 */
                hpdisplaytype = 1;
                /* 491 */
            } else if (mobId >= 9300184 && mobId <= 9300215) {
                /* 492 */
                hpdisplaytype = 2;
                /* 493 */
            } else if (!stats.isBoss() || mobId == 9410066 || stats.isPartyBonus()) {
                /* 494 */
                hpdisplaytype = 3;
            }
            /* 496 */
            stats.setHPDisplayType(hpdisplaytype);

            /* 498 */
            monsterStats.put(Integer.valueOf(mobId), stats);
        }
        /* 500 */
        return stats;
    }

    public static void decodeElementalString(MapleMonsterStats stats, String elemAttr) {
        /* 504 */
        for (int i = 0; i < elemAttr.length(); i += 2) {
            /* 505 */
            stats.setEffectiveness(Element.getFromChar(elemAttr.charAt(i)), ElementalEffectiveness.getByNumber(Integer.valueOf(String.valueOf(elemAttr.charAt(i + 1))).intValue()));
        }
    }

    private static boolean isDmgSponge(int mid) {
        /* 510 */
        switch (mid) {
            case 8810018:
            case 8810118:
            case 8810119:
            case 8810120:
            case 8810121:
            case 8810122:
            case 8820009:
            case 8820010:
            case 8820011:
            case 8820012:
            case 8820013:
            case 8820014:
            case 8820108:
            case 8820109:
            case 8820110:
            case 8820111:
            case 8820112:
            case 8820113:
            case 8820114:
            case 8820300:
            case 8820301:
            case 8820302:
            case 8820303:
            case 8820304:
                /* 535 */
                return true;
        }
        /* 537 */
        return false;
    }

    public static MapleNPC getNPC(int nid, int mapid) {
        /* 541 */
        String name = getNpcName(nid);
        /* 542 */
        if (name == null) {
            /* 543 */
            return null;
        }
        /* 545 */
        return new MapleNPC(nid, name, mapid);
    }

    public static String getNpcName(int nid) {
        /* 549 */
        return allNpcNames.get(Integer.valueOf(nid));
    }

    public static Map<Integer, String> getAllNpcName() {
        /* 553 */
        return allNpcNames;
    }

    public static int getRandomNPC() {
        /* 557 */
        int ret = 0;
        /* 558 */
        List<Integer> vals = new ArrayList<>(allNpcNames.keySet());
        /* 559 */
        while (ret <= 0) {
            /* 560 */
            ret = ((Integer) vals.get(Randomizer.nextInt(vals.size()))).intValue();
            /* 561 */
            if (((String) allNpcNames.get(Integer.valueOf(ret))).contains("MISSINGNO")) {
                /* 562 */
                ret = 0;
            }
        }
        /* 565 */
        return ret;
    }

    public static class loseItem {
        private final int id;
        private final byte chance;
        private final byte x;

        private loseItem(int id, byte chance, byte x) {
            /* 575 */
            this.id = id;
            /* 576 */
            this.chance = chance;
            /* 577 */
            this.x = x;
        }

        public int getId() {
            /* 581 */
            return this.id;
        }

        public byte getChance() {
            /* 585 */
            return this.chance;
        }

        public byte getX() {
            /* 589 */
            return this.x;
        }
    }
}
